﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using SSS.AlphaNN.Components;

namespace AlphaNN.Forms
{
    public partial class WeightsGenerationForm : Form
    {
        private List<Link> _link;

        private Random rnd;
        public WeightsGenerationForm(List<Link> GetLink)
        {
            InitializeComponent();
            _link = GetLink;
            rnd = new Random();
        }

        /// <summary>
        /// Кнопка генерирует веса и закрывает форму
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGenerate_Click(object sender, EventArgs e)
        {
           GenerateValues( sender, e );
           Close();
        }

        private void rbInteger_Click(object sender, EventArgs e)
        {
            rbInteger.Checked = true;
            rbReal.Checked = false;
        }

        private void rbReal_Click(object sender, EventArgs e)
        {
            rbReal.Checked = true;
            rbInteger.Checked = false;
        }

        private double CreateDoubleWeight(double min, double max)
        {
            double res = new double();
            do
            {
                res = rnd.Next((int)min, (int)max) + rnd.NextDouble();
            }
            while ((res < min) || (res > max));
            return res;
        }

        private int CreateIntegerWeight(int min, int max)
        {
            int res = new int();
            res = rnd.Next(min, max);
            return res;
        }

        private void rbReal_CheckedChanged(object sender, EventArgs e)
        {
            tbMin.Text = "0";
            tbMax.Text = "1";
        }
        
        /// <summary>
        /// Кнопка отмена
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Undo( object sender, EventArgs e )
        {
            Close();
        }
        
        /// <summary>
        /// генерация весов
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GenerateValues( object sender, EventArgs e )
        {
            if ( rbReal.Checked )
            {
                double min;
                double max;

                if ( double.TryParse( tbMin.Text, out min ) && double.TryParse( tbMax.Text, out max ) )
                {
                    _link.ForEach( x => x.ValueLink = CreateDoubleWeight( Math.Min( min, max ), Math.Max( min, max ) ) );
                }
                else
                {
                    MessageBox.Show( "Проверьте введенные данные",
                        "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error );
                }
            }
            else
            {
                int min;
                int max;

                if ( int.TryParse( tbMin.Text, out min ) && int.TryParse( tbMax.Text, out max ) )
                {
                    _link.ForEach( x => x.ValueLink = CreateIntegerWeight( Math.Min( min, max ), Math.Max( min, max ) ) );
                }
                else
                {
                    MessageBox.Show( "Проверьте введенные данные",
                        "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error );
                }
            }
        }
    }
}
